Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Microsoft.WindowsAPICodePack.Dialogs

Public Class TaskDialogSupport
    Public Shared Function MessagePrompt(ByVal tz As TimeZoneInfo) As Boolean
        ' Common buttons sample
        Dim tdCommonButtons As New TaskDialog()
        tdCommonButtons.Cancelable = False
        tdCommonButtons.Caption = "Time zone change"
        tdCommonButtons.Text = "You have entered the " + tz.DisplayName + " time zone"
        tdCommonButtons.InstructionText = "Do you want to update to this time zone?"

        tdCommonButtons.StandardButtons = TaskDialogStandardButtons.Yes Or TaskDialogStandardButtons.No

        Dim tdr As TaskDialogResult = tdCommonButtons.Show()
        Return (tdr = TaskDialogResult.Yes)
    End Function

End Class

Public Class TimezonePickerDialog
    Inherits TaskDialog
    Private clickHandler As EventHandler

    Dim _selectedTimezone As TimeZoneInfo
    Public Property SelectedTimezone() As TimeZoneInfo
        Get
            Return _selectedTimezone
        End Get
        Set(ByVal value As TimeZoneInfo)
            _selectedTimezone = value
        End Set
    End Property

    Public Sub New()
        Me.Caption = "Time zone change"
        Me.Text = "Your current location matches more than one potential time zone."
        Me.InstructionText = "Please select the correct time zone or click Cancel:"
        Me.FooterText = "Time zone changer -- MEF Utility Runner"
        Me.Cancelable = True

        ' Add a close button so user can close our dialog
        Me.StandardButtons = TaskDialogStandardButtons.Cancel

        clickHandler = New EventHandler(AddressOf tdcl_Click)
    End Sub

    Public Function Prompt(ByVal zones As TimeZoneInfo()) As TimeZoneInfo
        SelectedTimezone = Nothing
        Me.Controls.Clear()

        'using (new EnableTaskDialogInScope(true))
        ' Creating and adding command link buttons

        For Each tz As TimeZoneInfo In zones
            Dim tdcl As New TaskDialogCommandLink(tz.Id, tz.DisplayName)
            AddHandler tdcl.Click, clickHandler

            Me.Controls.Add(tdcl)
        Next

        ' Show the taskdialog
        Dim result As TaskDialogResult = Me.Show()
        If result = TaskDialogResult.Cancel Then
            Return Nothing
        Else
            Return SelectedTimezone
        End If

    End Function


    Sub tdcl_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim tdcl As TaskDialogCommandLink = TryCast(sender, TaskDialogCommandLink)
        If Not tdcl Is Nothing Then
            SelectedTimezone = TimeZoneInfo.FindSystemTimeZoneById(tdcl.Name)
            Me.Close(TaskDialogResult.Ok)
        End If
    End Sub

End Class
